package com.superman.supermarket.client.widget.textarea;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 多行输入文本框
 * 
 * @author superman
 * @version 2018年8月3日下午4:00:55
 */
public class MyTextArea extends Composite {

	interface MyTextAreaUiBinder extends UiBinder<Widget, MyTextArea> {
	}

	private static MyTextAreaUiBinder uiBinder = GWT.create(MyTextAreaUiBinder.class);

	@UiField
	FlowPanel inputbase;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月3日 下午4:00:57
	 */
	public MyTextArea() {
		initWidget(uiBinder.createAndBindUi(this));

		inputbase.getElement().setAttribute("contenteditable", "true");
		inputbase.addDomHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				event.stopPropagation();
				inputbase.getElement().focus();
				getSelection(inputbase.getElement());
			}
		}, ClickEvent.getType());
	}

	private native void getSelection(Element obj)/*-{
													if ($wnd.getSelection) {//ie11 10 9 ff safari
													var range = $wnd.getSelection();//创建range
													range.selectAllChildren(obj);//range 选择obj下所有子内容
													range.collapseToEnd();//光标移至最后
													}
													}-*/;

}
